package com.itheima.proxy;
import com.itheima.service.UserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserServiceJDKProxy {
    public   static    UserService createJDKProxy(final UserService userService){
//        获取被代理对象的类加载器
        ClassLoader classLoader = userService.getClass().getClassLoader();
//        获取被代理对象的实现的接口
        Class[] classes = userService.getClass().getInterfaces();
//         对原始方法拦截并增强
       InvocationHandler ih= new InvocationHandler(){

           public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
               System.out.println("前置通知");
               Object ret = method.invoke(userService, args);
               System.out.println("后置通知");
               return ret;
           }
       };

        UserService userService1=(UserService) Proxy.newProxyInstance(classLoader,classes,ih);
        return userService1;
    }
}
